<?php

	abstract class AbstractSocialNetworkAlert extends AppModel
	{
		const TYPE_FACEBOOK = 'Facebook';
		const TYPE_TWITTER  = 'Twitter';
		const TYPE_LINKEDIN = 'Linkdein';
		
		/**
		 * 
		 * @param $userId
		 * @param $event
		 * @param $alertType
		 */
		function allowAlert($userId,$event,$alertType)
		{
			return $this->hasAny(array('user_id'=>$userId,'event'=>$event,'alert_type'=>$alertType,'is_active'=>1));
		}
		
		function afterInitialize()
		{
			$this->makeFieldProperties();
			parent::afterInitialize();
		}
		/**
		 * 
		 * @param $userId
		 * @param $event
		 * @param $message
		 */
		function sendMessage($userId,$event,$message)
		{
			if($this->allowAlert($userId,$event,self::TYPE_FACEBOOK))
			{
				$this->sendFacebookMessage($userId,$event,$message);		
			}
			if($this->allowAlert($userId,$event,self::TYPE_TWITTER))
			{
				$this->sendTwitterMessage($userId,$event,$message);		
			}
			if($this->allowAlert($userId,$event,self::TYPE_LINKEDIN))
			{
				$this->sendLinkedinMessage($userId,$event,$message);
			}
		}
		
	function sendFacebookMessage($userId,$event,$message)
	{
		cri('Sociable.FacebookAccount')->postStatus($userId,$message);
	}
	
	function sendTwitterMessage($userId,$event,$message)
	{
		cri('Sociable.TwitterAccount')->postStatus($userId,$message);
	}
	
	function sendLinkedinMessage($userId,$event,$message)
	{
		cri('Sociable.LinkedinAccount')->postStatus($userId,$message);	
	}
	
	abstract function getEvents();
	
	/**
	 * return info about alert for a user
	 * @param $userId
	 * @param $alertTypes
	 */
	function getEventInfo($userId,$alertTypes=null)
	{
		$eventInfo = array();
		if(is_null($alertTypes))
		{
			$alertTypes = $this->getActiveAlerts();
		}
		$events = $this->getEvents();
		foreach((array)$alertTypes as $type)
		{
			$subscribedEvents = $this->find('list',array('conditions'=>array('user_id'=>$userId,'alert_type'=>$type,'is_active'=>1),'fields'=>array('event')));
			foreach($events as $event)
			{
				$value = 0;
				if(in_array($event,$subscribedEvents))
				{
					$value = 1;
				}
				$eventInfo[$type.'_'.$event] = $value;
			}
			
		}
		return array($this->alias => $eventInfo);
		
	}
	
	/**
	 * make fieldproperties based on events 
	 */
	function makeFieldProperties()
	{
		$events = $this->getEvents();
		$activatAlerts = $this->getActiveAlerts();
		foreach($activatAlerts as $alertType)
		{
			foreach($events as $event)
			{
				$field  = "$this->alias.".$alertType."_".$event;
				$this->setFieldProperties($field,array('type'=>"checkbox",'group'=>array($alertType)));
			}
		}
	}
	/**
	 * Return list of alert to used
	 */
	function getActiveAlerts()
	{
		return array(self::TYPE_FACEBOOK,self::TYPE_TWITTER,self::TYPE_LINKEDIN);
	}
	/**
	 * 
	 */
	function saveAlerts($userId,$data)
	{
		if(isset($data[$this->alias]))
		{
			$data = $data[$this->alias];
		}
		foreach($data as  $eventInfo => $value)
		{
			$nameParts = explode('_',$eventInfo);
			$alertType = array_shift($nameParts);
			$eventName = array_pop($nameParts);
			if(in_array($alertType,$this->getActiveAlerts(),true) == false || !in_array($eventName,$this->getEvents(),true))
			{
				continue;
			}
			if($value != 0)
				$value = 1;
			$id = $this->field('id',array('user_id'=>$userId,'event'=>$eventName,'alert_type'=>$alertType));
			$this->create();
			$alertData = array();
			$alertData[$this->alias] = array('user_id'=>$userId,'event'=>$eventName,'alert_type'=>$alertType,'is_active'=>$value);
			if($id != false)
			{
				$this->id = $id;
			}
			$this->save($alertData);
		}
		return true;
	}
	
	abstract function postMessage($userId,$event,$data=array());
	
	function getSocialAccountModel($type)
	{
		switch($type)
		{
			case self::TYPE_FACEBOOK:
				break;
			case self::TYPE_TWITTER:
				break;
			case self::TYPE_LINKEDIN:
				break;
		}
	}
}
?>